App({
  globalData:{
    backend_add:'http://localhost:8080/',
    token:null,
    avatar_url: 'https://img1.baidu.com/it/u=153738116,1571893543&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
    nickname: '匿名用户',
  },
  onLaunch() {

    // 先判断缓存是否存在Token
    let userToken = wx.getStorageSync('userToken');
    if (!userToken) { // Toekn不存在
      this.userLogin();
    } else { // 缓存有Token
      wx.checkSession({
        fail: () => {
          this.userLogin();
        }
      })
    }
  },
  userLogin() {
    wx.login({
      success: res => {
        console.log(res.code);
        if (res.code) {
          wx.request({
            url: this.globalData.backend_add+'wechat/login',
            method: 'POST',
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              code: res.code
            },
            success: loginRes => {
              if (loginRes.data.token) {
                console.log("请求成功:"+loginRes.data.code+" "+loginRes.data.msg);
                console.log("userToken:"+loginRes.data.token);
                this.globalData.userToken = loginRes.data.token;
                wx.setStorageSync('userToken', loginRes.data.token);
              }
            }
          });
        }
      }
    })
  },
})